import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { UserDto } from './dto/user.dto';
import { User } from './user.entity';

// export type User = any;

@Injectable()
export class UserService {
  private readonly users: User[];

  constructor(
    @InjectModel(User)
    private readonly userRepository: typeof User,
  ) {}

  async findAll() {
    const users = await this.userRepository.findAll<User>();
    return users.map((user) => new UserDto(user));
  }

  async findOne(username: string): Promise<User | undefined> {
    const user = await this.userRepository.findOne({
      where: {
        username,
      },
    });
    return user;
  }

  async getNumber(): Promise<number> {
    const length = await this.userRepository.count();
    return length;
  }

  async getUserRoles(uuid: string): Promise<string> {
    const user = await this.userRepository.findOne({
      where: {
        uuid,
      },
    });
    return user.roles;
  }
}
